package com.richapp.launcher.database.system;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.richapp.launcher.database.system.dao.BoxModelDao;
import com.richapp.launcher.database.system.dao.RoomModelDao;
import com.richapp.launcher.database.system.dao.SystemValueModelDao;
import com.richapp.launcher.database.system.mode.BoxLogin;
import com.richapp.launcher.database.system.mode.BoxModel;
import com.richapp.launcher.database.system.mode.BoxPhone;
import com.richapp.launcher.database.system.mode.BoxScreen;
import com.richapp.launcher.database.system.mode.UserLogin;
import com.richapp.launcher.database.system.mode.UserMachine;
import com.richapp.launcher.database.system.mode.UserMachineButton;
import com.richapp.launcher.database.system.mode.ActionInfraredModel;
import com.richapp.launcher.database.system.mode.ActionModel;
import com.richapp.launcher.database.system.mode.ConditionSensorModel;
import com.richapp.launcher.database.system.mode.ConditionStatusModel;
import com.richapp.launcher.database.system.mode.ConditionTimeModel;
import com.richapp.launcher.database.system.mode.DeviceDayPowerConsumption;
import com.richapp.launcher.database.system.mode.DeviceModel;
import com.richapp.launcher.database.system.mode.DeviceModelHistory;
import com.richapp.launcher.database.system.mode.RoomModel;
import com.richapp.launcher.database.system.mode.SceneModel;
import com.richapp.launcher.database.system.mode.SystemValueModel;
import com.richapp.launcher.database.system.mode.UserModel;
import com.richapp.launcher.serialport.gateway.GatewaytConstant;

public class SystemDatabaseHelper extends OrmLiteSqliteOpenHelper {

	private static final int databaseVersion =41;
	private static SystemDatabaseHelper instance;
	private static final String TAG = SystemDatabaseHelper.class.getName();
	
	private Map<String, Dao> daos = new HashMap<String, Dao>();
	
	private static Context contextObj = null;
	
	/**
	 * 获取单例helper
	 * @param context
	 * @return
	 */
	public static synchronized SystemDatabaseHelper getHelper(Context context){
		if(instance == null){
			synchronized (SystemDatabaseHelper.class) {
				if(instance == null){
					contextObj = context;
					instance = new SystemDatabaseHelper(contextObj);
				}
			}
		}
		return instance;
	}
	
	private SystemDatabaseHelper(Context context) {
		super(context, "/data/data/"+context.getPackageName()+"/databases/smartHomeLauncher.db", null, databaseVersion);
	}

	
	@Override
	public void onCreate(SQLiteDatabase sd, ConnectionSource cs) {
		try {
			//创建表
			TableUtils.createTableIfNotExists(cs, UserModel.class);
			TableUtils.createTableIfNotExists(cs, UserLogin.class);
			TableUtils.createTableIfNotExists(cs, RoomModel.class);
			TableUtils.createTableIfNotExists(cs, DeviceModel.class);
			TableUtils.createTableIfNotExists(cs, BoxModel.class);
			TableUtils.createTableIfNotExists(cs, BoxLogin.class);
			TableUtils.createTableIfNotExists(cs, BoxPhone.class);
			TableUtils.createTableIfNotExists(cs, DeviceModelHistory.class);
			TableUtils.createTableIfNotExists(cs, DeviceDayPowerConsumption.class);
			TableUtils.createTableIfNotExists(cs, SystemValueModel.class);
			TableUtils.createTableIfNotExists(cs, BoxScreen.class);


            //情景
			TableUtils.createTableIfNotExists(cs, ActionModel.class);
			TableUtils.createTableIfNotExists(cs, ActionInfraredModel.class);
			TableUtils.createTableIfNotExists(cs, ConditionSensorModel.class);
			TableUtils.createTableIfNotExists(cs, ConditionStatusModel.class);
			TableUtils.createTableIfNotExists(cs, ConditionTimeModel.class);
			TableUtils.createTableIfNotExists(cs, SceneModel.class);
            //红外
            TableUtils.createTableIfNotExists(cs, UserMachine.class);
            TableUtils.createTableIfNotExists(cs, UserMachineButton.class);

			//添加默认房间数据
			RoomModelDao rmd = new RoomModelDao(contextObj);
			RoomModel rm = new RoomModel();
			rm.setRoomName("厨房");
			rmd.add(rm);
			rm = new RoomModel();
			rm.setRoomName("客厅");
			rmd.add(rm);
			rm = new RoomModel();
			rm.setRoomName("卫生间");
			rmd.add(rm);
			rm = new RoomModel();
			rm.setRoomName("主卧");
			rmd.add(rm);
			rm = new RoomModel();
			rm.setRoomName("次卧");
			rmd.add(rm);
			rm = new RoomModel();
			rm.setRoomName("阳台");
			rmd.add(rm);

			//赛天版本： 电竟区 .游戏对战区 .女生区 .大厅区 .贵宾区 . 休闲游戏区
			rm = new RoomModel();
			rm.setRoomName("电竟区");
			rmd.add(rm);
			rm = new RoomModel();
			rm.setRoomName("游戏对战区");
			rmd.add(rm);
			rm = new RoomModel();
			rm.setRoomName("女生区");
			rmd.add(rm);
			rm = new RoomModel();
			rm.setRoomName("大厅区");
			rmd.add(rm);
			rm = new RoomModel();
			rm.setRoomName("贵宾区");
			rmd.add(rm);
			rm = new RoomModel();
			rm.setRoomName("休闲游戏区");
			rmd.add(rm);
			
			//添加默认系统属性
			SystemValueModelDao svmd = new SystemValueModelDao(contextObj);
			BoxModelDao boxModelDao = new BoxModelDao(contextObj);
			//布防状态
			SystemValueModel svm = new SystemValueModel();
			svm.setName(GatewaytConstant.SYSTEM_KEY_NAME_ALARM_FLAG);
			svm.setBoxCpuId(boxModelDao.getCurrentBox().getBoxCpuId());
			svm.setValue("false");
			svmd.add(svm);
		} catch (Exception e) {
			Log.v(TAG, e.getMessage());
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase sd, ConnectionSource cs, int oldVersion,int newVersion) {
		try {
			//删除表
			TableUtils.dropTable(cs, UserModel.class, true);
			TableUtils.dropTable(cs, UserLogin.class,true);
			TableUtils.dropTable(cs, RoomModel.class, true);
			TableUtils.dropTable(cs, DeviceModel.class, true);
			TableUtils.dropTable(cs, BoxModel.class, true);
			TableUtils.dropTable(cs, BoxLogin.class, true);
			TableUtils.dropTable(cs,BoxPhone.class,true);
			TableUtils.dropTable(cs, DeviceModelHistory.class, true);
			TableUtils.dropTable(cs, DeviceDayPowerConsumption.class,true);
			TableUtils.dropTable(cs, SystemValueModel.class, true);
			TableUtils.dropTable(cs,BoxScreen.class,true);

			TableUtils.dropTable(cs, ActionModel.class, true);
			TableUtils.dropTable(cs, ActionInfraredModel.class, true);
			TableUtils.dropTable(cs, ConditionSensorModel.class, true);
			TableUtils.dropTable(cs, ConditionStatusModel.class, true);
			TableUtils.dropTable(cs, ConditionTimeModel.class, true);
			TableUtils.dropTable(cs, SceneModel.class, true);

            TableUtils.dropTable(cs,UserMachine.class,true);
            TableUtils.dropTable(cs,UserMachineButton.class,true);
			
			//创建表
			onCreate(sd, cs);
		} catch (Exception e) {
			Log.v(TAG, e.getMessage());
		}
	}
	
	
	public synchronized Dao getDao(Class clazz) throws SQLException
    {  
        Dao dao = null;  
        String className = clazz.getSimpleName();  
  
        if (daos.containsKey(className))  
        {  
            dao = daos.get(className);  
        }  
        if (dao == null)  
        {  
            dao = super.getDao(clazz);  
            daos.put(className, dao);  
        }  
        return dao;  
    }

	/** 
     * 释放资源 
     */  
    public void close()
    {  
        super.close();  
  
        for (String key : daos.keySet())  
        {  
            Dao dao = daos.get(key);  
            dao = null;  
        }  
    }  
}
